package com.aos.controller;

import com.aos.dao.UserDao;
import com.aos.entity.User;
import com.aos.exception.BusinessException;
import com.aos.exception.ResultStatus;
import com.aos.service.role.RoleService;
import com.aos.service.user.UserService;
import com.aos.service.user_role.UserRoleService;
import com.aos.util.AssertUtil;
//import com.aos.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by quxiaoyan
 * 用户数据同步接口
 */
@RestController
@RequestMapping("/user")
@Api(value = "User Controller", tags = {"用户体系接口"})
public class UserController {

    private static Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userService;

    @Autowired
    RoleService roleService;

    @Autowired
    UserDao userDao;

    @Autowired
    UserRoleService userRoleService;

    /**
     * 用户数据同步
     * @param {String} userName 用户名
     * @param {String} password 密码
     * @param {String} realName 中文名
     * @param {String} serviceId 服务名
     * @param {String} dataType 数据操作类型
     * @param {String} loginType 登录类型
     * @return Object
     */
    @ApiOperation(value = "用户体系数据同步", notes = "数据同步")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名",   required = true,paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "realname", value = "中文名",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "service_id", value = "服务名",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "data_type", value = "数据操作类型", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "login_type", value = "登录类型", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "uid", value = "用户标识ID", paramType = "query", dataType = "Long",example = "0"),
            @ApiImplicitParam(name = "login_type", value = "登录类型", paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/synchro", method = RequestMethod.POST)
    public Object synchro(@RequestParam(value = "username",required = false) String userName,
                            @RequestParam(value = "password",required = false) String password ,
                            @RequestParam(value = "realname",required = false) String realName ,
                            @RequestParam(value = "service_id",required = false) String serviceId,
                            @RequestParam(value = "data_type",required = false) String dataType,
                            @RequestParam(value = "login_type",required = false) String loginType,
                            @RequestParam(value = "uid",required = false) Long uid,
                            @RequestParam(value = "salt",required = false) String salt){

        AssertUtil.notNull(userName.trim(), "用户名为空");
        AssertUtil.notNull(serviceId, "服务名为空");
        AssertUtil.notNull(dataType, "数据操作类型为空");
        AssertUtil.notNull(loginType, "登录类型为空");
        //AssertUtil.notNull(salt, "加密盐为空");

        System.out.println("username:["+ userName +"]");
        System.out.println("serviceId:["+ serviceId +"]");
        System.out.println("dataType:["+ dataType +"]");
        System.out.println("dataType:["+ loginType +"]");

        String msg = "操作：";
        //判断传入的登录类型：PA-用户名/密码类型，FI-指纹，FA-面部识别，ID-身份证
        if(loginType.trim().equals("PA"))   //用户名/密码类型
        {
            //判断数据操作类型：insert-插入，update-修改，delete-删除
            if(dataType.trim().equals("insert"))
            {
                //用户数据插入
                int incount = userService.insertUser(userName,realName,password,serviceId,uid,salt);
                msg = msg + "数据新增成功" ;
            }
            else if(dataType.trim().equals("update")) //修改
            {
                //用户数据修改
                int upcount = userService.updateUser(userName,realName,password,serviceId,uid,salt);
                msg = msg + "数据修改成功" ;
            }
            else if(dataType.trim().equals("delete")) //删除
            {
                int decount = userService.deleteUser(userName,serviceId,uid);
                msg = msg + "数据删除成功" ;
            }
        }
        else if(loginType.trim().equals("FI"))//指纹
        {

        }
        else if(loginType.trim().equals("FA"))//面部识别
        {

        }
        else if(loginType.trim().equals("ID"))//身份证
        {

        }
        else{
            msg = msg + "登录类型错误，操作失败！";
            throw new BusinessException("登录类型错误，操作失败！", ResultStatus.fail.toString());
            //return Result.error("登录类型错误，操作失败！");
        }

        return msg;
    }

    /**
     * 角色数据同步
     * @param {String} roleName 角色名
     * @param {String} role_id 角色id
     * @return map
     */
//    @RequestMapping(value = "/role", method = RequestMethod.POST)
//    public Object role( @RequestParam(value = "role_id",required = false) Long roleId,
//                         @RequestParam(value = "role_name",required = false) String roleName,
//                         @RequestParam(value = "data_type",required = false) String dataType){
//
//        AssertUtil.notNull(roleId, "角色ID为空");
//        AssertUtil.notNull(roleName, "角色名为空");
//        AssertUtil.notNull(dataType, "数据操作类型为空");
//
//        String msg = "操作：";
//        if(dataType.trim().equals("insert"))//新增
//        {
//            int incount = roleService.insertRole(roleId,roleName);
//            msg = msg + "新增角色信息成功";
//        }
//        else if(dataType.trim().equals("update"))//修改
//        {
//            int upcount = roleService.updateRole(roleId,roleName);
//            msg = msg + "更新角色信息成功";
//        }
//        else if(dataType.trim().equals("delete"))//删除
//        {
//            int decount = roleService.deleteRole(roleId);
//            msg = msg + "删除角色信息成功";
//        }
//        return msg;
//    }

//    /**
//     * 用户角色信息同步
//     * @param {String} roleName 角色名
//     * @param {String} role_id 角色id
//     * @return map
//     */
//    @RequestMapping(value = "/user-role", method = RequestMethod.POST)
//    public Object userRole(@RequestParam(value = "username",required = false) String userName,
//                            @RequestParam(value = "role_id",required = false) Long roleId,
//                            @RequestParam(value = "service_id",required = false) String serviceId,
//                            @RequestParam(value = "data_type",required = false) String dataType){
//
//        AssertUtil.notNull(userName, "角色名为空");
//        AssertUtil.notNull(roleId, "角色ID为空");
//        AssertUtil.notNull(serviceId, "服务名为空");
//        AssertUtil.notNull(dataType, "数据操作类型为空");
//
//        String msg = "操作：";
//
//        //首先查询该用户的uid
//        User user = userDao.findByUsernameAndServiceid(userName,serviceId);
//        if(user == null)
//        {
//            throw new BusinessException("该用户ID不存在，请检查！", ResultStatus.fail.toString());
//        }
//        //用户信息存在，进行用户角色信息数据操作
//        if(dataType.trim().equals("insert"))//新增
//        {
//            int incount = userRoleService.insertUserRole(user.getUid(),userName,serviceId,roleId);
//            msg = msg + "用户角色信息新增成功";
//        }
//        else if(dataType.trim().equals("delete"))
//        {
//            int decount = userRoleService.deleteUserRole(user.getUid(),roleId);
//            msg = msg + "用户角色信息删除成功";
//        }
//        return msg;
//    }
}
